package org.corolla.cloth.web.action;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.corolla.cloth.compute.ComputTranspotFees;
import org.corolla.cloth.constdata.ClothSizeConst;
import org.corolla.cloth.entity.Accessory;
import org.corolla.cloth.entity.Order;
import org.corolla.cloth.service.ComputeService;
import org.corolla.cloth.service.OrderService;
import org.corolla.cloth.utils.DataUtil;

import com.opensymphony.xwork2.ActionSupport;

@ParentPackage(value = "struts-default")
@Results({
          @Result(name = "input", type = "redirect", location = "/view/PriceSheet.jsp"),
          @Result(name = "success", type = "redirect", location = "/view/PriceSheet.jsp") })
@Namespace("/view")
@Action(value = "pricesheetAction")
public class PriceSheetAction extends ActionSupport {

    @Resource(name = "orderService")
    private OrderService      orderService;

    /**
     * 
     */
    private static final long serialVersionUID = 1L;


    @Override
    public String execute() throws Exception {
    	ComputeService computeService=new ComputeService();
    	orderService=new OrderService();
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpSession session = request.getSession();
        // 程序运行到execute()时，前面的验证都已经正确了
        try {
            List<Order> orderlist = new ArrayList<Order>();
            Set<Accessory> accessorySet = new HashSet<Accessory>();
            if (StringUtils.isNotBlank(session.getAttribute("button").toString())
                && StringUtils.isNotBlank(session.getAttribute("buttonnum").toString())) {
                Accessory accessory = new Accessory(Double.valueOf(session.getAttribute("button").toString()),
                                                    Integer.valueOf(session.getAttribute("buttonnum").toString()));
                accessorySet.add(accessory);
            }
            if (StringUtils.isNotBlank(session.getAttribute("shoulder").toString())
                && StringUtils.isNotBlank(session.getAttribute("shouldernum").toString())) {
                Accessory accessory = new Accessory(Double.valueOf(session.getAttribute("shoulder").toString()),
                                                    Integer.valueOf(session.getAttribute("shouldernum").toString()));
                accessorySet.add(accessory);
            }
            // 面料价格
            double liningPrice = Double.valueOf(session.getAttribute("liningPrice").toString());
            String size[] = session.getAttribute("checksize").toString().split("_");
            String siznum[] = session.getAttribute("sizenum").toString().split("_");
            String unitPrice = "";
            double totelprice = 0.0;
            int clothcount = 0;
            for (int i = 0; i < size.length; i++) {

                // 获取单价
                double unitPriceBySize = computeService.getUnitPriceBysize(size[i], accessorySet,
                                                                           Integer.valueOf(siznum[i]),liningPrice);
                double total_price = unitPriceBySize*Integer.valueOf(siznum[i]);
                totelprice = totelprice + total_price;
                clothcount = clothcount + Integer.valueOf(siznum[i]);

                session.setAttribute(size[i] + "_unitPriceBySize", unitPriceBySize);
                session.setAttribute(size[i] + "_size", ClothSizeConst.getClothSizeMap().get(size[i]));
                if (i < size.length) {
                    unitPrice = unitPrice + String.valueOf(unitPriceBySize) + "_";
                } else {
                    unitPrice = unitPrice + String.valueOf(unitPriceBySize);
                }
                Order order =orderService.generateOrder(session.getAttribute("style_name").toString(), size[i],
                                        Integer.valueOf(siznum[i]), unitPriceBySize, total_price);
              
                orderlist.add(order);
            }
            session.setAttribute("orderPricelist",orderlist);

            double transportfees = ComputTranspotFees.getTransportFees(clothcount);
            double totalcountprice = (transportfees + totelprice);
            session.setAttribute("totalcountprice", totalcountprice);
            session.setAttribute("transportfees", transportfees);
            session.setAttribute("totelprice", totelprice);
           
            return SUCCESS;
        } catch (Exception ex) {
            ex.printStackTrace();
            return "input";
        }

    }
}
